package com.xzz;

/**
 * @author: hhz
 * @create: 2021-12-09 10:04
 **/
public class SearchInsert {
    public static void main(String[] args) {
        int[] nums = new int[]{1,3,5,6};
        System.out.println(searchInsert(nums,5));
        System.out.println(searchInsert(nums,2));
        System.out.println(searchInsert(nums,28));
    }
    public static int searchInsert(int[] nums,int target){
        int left = 0,right = nums.length;
        while(right>left){
            int mid = left+ (right-left)/2;
            if(nums[mid]>target){
                right = mid;
            }else if(nums[mid]<target){
                left = mid+1;
            }else {
                right = mid;
            }
        }
        return right;

    }
}
